"use strict";
cc._RF.push(module, '1ef25j04mJLp6+At8h0xDRi', 'PlayerManager');
// Script/Managers/PlayerManager.ts

"use strict";
var __extends = (this && this.__extends) || (function () {
    var extendStatics = function (d, b) {
        extendStatics = Object.setPrototypeOf ||
            ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
            function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
        return extendStatics(d, b);
    };
    return function (d, b) {
        extendStatics(d, b);
        function __() { this.constructor = d; }
        d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
    };
})();
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
    var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
    if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
    else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
    return c > 3 && r && Object.defineProperty(target, key, r), r;
};
Object.defineProperty(exports, "__esModule", { value: true });
var _a = cc._decorator, ccclass = _a.ccclass, property = _a.property;
var AppDef_1 = require("../Core/AppDef");
var AppEvent_1 = require("../Core/AppEvent");
var Player_1 = require("../Player");
var EventManager_1 = require("./EventManager");
var PlayerManager = /** @class */ (function (_super) {
    __extends(PlayerManager, _super);
    function PlayerManager() {
        var _this = _super !== null && _super.apply(this, arguments) || this;
        _this.playerPrefab = null;
        _this.enemyPrefab = null;
        _this.roleLayer = null;
        _this.player = null;
        _this.tempJoyStick = null;
        _this.playerList = [[], []];
        _this.collectPlayerPool = [[], []];
        _this.maxNum = 10;
        return _this;
    }
    PlayerManager.prototype.onLoad = function () {
        EventManager_1.addListener(AppEvent_1.EVT_PLAYER_SET_P, this.playerSetP, this);
        EventManager_1.addListener(AppEvent_1.EVT_PLAYER_SET_WEAPON_P, this.playerSetWeaponP, this);
        EventManager_1.addListener(AppEvent_1.EVT_CREATE_PLAYER, this.createPlayer, this);
        EventManager_1.addListener(AppEvent_1.EVT_PLAYER_HIT, this.PlayerHit, this);
    };
    PlayerManager.prototype.createPlayer = function (event) {
        var data = event.getUserData();
        var player = this.checkCollectList(data.playerType);
        player.position = cc.v3(data.posx, data.posy, 0);
        player.name = "player.id=" + this.playerList[data.playerType].length + " playerType=" + data.playerType;
        player.getComponent(Player_1.default).init(data.playerType, data.weapontype, '');
        this.roleLayer.addChild(player);
        this.playerList[data.playerType].push(player);
    };
    PlayerManager.prototype.checkCollectList = function (playerType) {
        var length = this.collectPlayerPool[playerType].length;
        if (length > 0) {
            var node = this.collectPlayerPool[playerType][length - 1];
            this.collectPlayerPool[playerType].pop();
            return node;
        }
        else {
            if (playerType == AppDef_1.PLAYER_TYPE.My) {
                this.player = cc.instantiate(this.playerPrefab);
                return this.player;
            }
            else {
                return cc.instantiate(this.enemyPrefab);
            }
        }
    };
    PlayerManager.prototype.collectPlayer = function (player) {
        if (this.collectPlayerPool[player.playerType].length >= this.maxNum) {
            player.destroy();
        }
        else {
            player.active = false;
            this.collectPlayerPool[player.playerType].push(player);
        }
    };
    PlayerManager.prototype.playerSetP = function (event) {
        var data = event.getUserData();
        if (data && data.p) {
            this.player.getComponent(Player_1.default).setP(data.p);
        }
    };
    PlayerManager.prototype.playerSetWeaponP = function (event) {
        var data = event.getUserData();
        this.player.getComponent(Player_1.default).setWeaponP(data);
    };
    PlayerManager.prototype.PlayerHit = function (event) {
        var data = event.getUserData();
        var isFind = false;
        for (var i = 0; i < this.playerList.length; i++) {
            for (var j = 0; j < this.playerList[i].length; j++) {
                var item = this.playerList[i][j];
                if (item.name === data.playerName) {
                    var scrtpt = item.getComponent(Player_1.default);
                    scrtpt.setHpReduce(10);
                    if (scrtpt.getPlayerHp() <= 0) {
                        this.collectPlayer(item);
                        isFind = true;
                        if (j !== -1) {
                            this.playerList.splice(j, 1);
                        }
                        break;
                    }
                }
            }
            if (isFind) {
                break;
            }
        }
    };
    __decorate([
        property(cc.Prefab)
    ], PlayerManager.prototype, "playerPrefab", void 0);
    __decorate([
        property(cc.Prefab)
    ], PlayerManager.prototype, "enemyPrefab", void 0);
    __decorate([
        property(cc.Node)
    ], PlayerManager.prototype, "roleLayer", void 0);
    PlayerManager = __decorate([
        ccclass
    ], PlayerManager);
    return PlayerManager;
}(cc.Component));
exports.default = PlayerManager;

cc._RF.pop();